//
//  TransformToPinyinVC.m
//  objective_c_language
//
//  Created by 龙 on 2018/5/23.
//  Copyright © 2018年 longlong. All rights reserved.
//

#define CellIdentifer @"UITableViewCell"

#import "TransformToPinyinVC.h"
#import "PinYin4Objc.h"

@interface TransformToPinyinVC ()
@property (nonatomic ,strong) NSArray<NSString *> *titleArray;
@end

@implementation TransformToPinyinVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellIdentifer];
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    cell.textLabel.text = self.titleArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSTimeInterval startTime=[[NSDate date] timeIntervalSince1970];
    
    for (int i = 0; i < 1000; i ++)
    {
        if (indexPath.row == 0)
        {
            [self getPinYinBySystemMethod];//0.607948
        }
        else
        {
            [self getPinYinByThirdMethod];//1.281216
        }
    }
    
    NSTimeInterval endTime=[[NSDate date] timeIntervalSince1970];
    NSTimeInterval time = endTime - startTime;
    NSLog(@"汉字换拼音耗时-------------- %f",time);

}

#pragma mark - Private Methods

- (void)getPinYinByThirdMethod
{
    NSString *sourceText=@"中国四川";
    for (int i = 1; i < 1000; i ++)
    {
        HanyuPinyinOutputFormat *outputFormat=[[HanyuPinyinOutputFormat alloc] init];
        [outputFormat setToneType:ToneTypeWithoutTone];
        [outputFormat setVCharType:VCharTypeWithV];
        [outputFormat setCaseType:CaseTypeLowercase];
        NSString *pinYin = [PinyinHelper toHanyuPinyinStringWithNSString:sourceText withHanyuPinyinOutputFormat:outputFormat withNSString:@" "];
        NSLog(@"pinYin ------ %@",pinYin);
    }
}

- (void)getPinYinBySystemMethod
{
    NSString *hanziText = @"中国四川";
    
    for (int i = 0 ; i < 100; i ++)
    {
        // 将中文字符串转成可变字符串
        NSMutableString *pinyinText = [[NSMutableString alloc] initWithString:hanziText];
        
        // 先转换为带声调的拼音
        CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformMandarinLatin, NO);
        NSLog(@"pinyin: %@", pinyinText); // 输出 pinyin: zhōng guó sì chuān
        
        // 再转换为不带声调的拼音
        CFStringTransform((__bridge CFMutableStringRef)pinyinText, 0, kCFStringTransformStripDiacritics, NO);
        NSLog(@"pinyin: %@", pinyinText); // 输出 pinyin: zhong guo si chuan
    }
}


#pragma mark - Getters And Setters

//注解：OC 中我们很习惯的会用懒加载，当用到的时候才去加载它，但是实际上只要一个类实现了某个方法，就会被加载进内存。
- (NSArray<NSString *> *)titleArray
{
    if (_titleArray == nil)
    {
        _titleArray = @[@"系统方法",@"自定义方法"];
    }
    
    return _titleArray;
}


@end


